//
//  ST_GuardHerNowAlertViewController.swift
//  btvideo
//
//  Created by Mac on 2021/4/28.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyAttributes
import SwiftyJSON

class ST_GuardHerNowAlertViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .clear
        
        view.addSubview(root_view)
        root_view.snp.makeConstraints{(make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(15)
            make.height.equalTo(420+15+BTSafeAreaBottom)
        }
        
        view.addSubview(close_view)
        close_view.snp.makeConstraints { (make) in
            make.top.right.left.equalToSuperview()
            make.bottom.equalTo(root_view.snp.top)
        }
        
        root_view.addSubview(topbackImg)
        topbackImg.snp.makeConstraints{(make) in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(126)
        }
        
        topbackImg.addSubview(herHeadImg)
        herHeadImg.snp.makeConstraints{(make) in
            make.centerY.equalToSuperview()
            make.centerX.equalToSuperview().offset(-43)
            make.width.height.equalTo(86)
        }
        
        topbackImg.addSubview(meHeadImg)
        meHeadImg.snp.makeConstraints{(make) in
            make.centerY.equalToSuperview()
            make.centerX.equalToSuperview().offset(43)
            make.width.height.equalTo(86)
        }
        
        topbackImg.addSubview(heart_image_view)
        heart_image_view.snp.makeConstraints{(make) in
            make.centerY.centerX.equalToSuperview()
            make.width.height.equalTo(46)
        }
        
        root_view.addSubview(titleLab)
        titleLab.snp.makeConstraints{(make) in
            make.top.equalTo(topbackImg.snp.bottom).offset(20)
            make.centerX.equalToSuperview()
        }
        
        root_view.addSubview(leftImg)
        leftImg.snp.makeConstraints{(make) in
            make.right.equalTo(titleLab.snp.left).offset(-5)
            make.centerY.equalTo(titleLab)
        }
        
        root_view.addSubview(rightImg)
        rightImg.snp.makeConstraints{(make) in
            make.left.equalTo(titleLab.snp.right).offset(5)
            make.centerY.equalTo(titleLab)
        }
        
        
        
        root_view.addSubview(onlyImg)
        onlyImg.snp.makeConstraints{(make) in
            make.centerX.equalToSuperview()
            make.width.height.equalTo(75)
            make.top.equalTo(heart_image_view.snp.bottom).offset(101)
        }
        
        root_view.addSubview(onlyLab)
        onlyLab.snp.makeConstraints{(make) in
            make.centerX.equalTo(onlyImg)
            make.top.equalTo(onlyImg.snp.bottom).offset(5)
            make.height.equalTo(20)
        }
        
        root_view.addSubview(onlyPlaceLab)
        onlyPlaceLab.snp.makeConstraints{(make) in
            make.centerX.equalTo(onlyLab)
            make.width.equalTo(100)
            make.top.equalTo(onlyLab.snp.bottom).offset(5)
        }
        
        root_view.addSubview(topImg)
        topImg.snp.makeConstraints{(make) in
            make.centerY.height.width.equalTo(onlyImg)
            make.left.equalTo(43)
        }
        
        root_view.addSubview(topLab)
        topLab.snp.makeConstraints{(make) in
            make.top.height.equalTo(onlyLab)
            make.centerX.equalTo(topImg)
        }
        
        root_view.addSubview(topPlaceLab)
        topPlaceLab.snp.makeConstraints{(make) in
            make.top.width.equalTo(onlyPlaceLab)
            make.centerX.equalTo(topImg)
        }
        
        root_view.addSubview(headerImg)
        root_view.addSubview(headframeImg)
        
        headframeImg.snp.makeConstraints{(make) in
            make.centerY.width.height.equalTo(topImg)
            make.right.equalToSuperview().offset(-35)
        }
        
        
        headerImg.snp.makeConstraints{(make) in
            make.center.equalTo(headframeImg)
            make.width.height.equalTo(57)
        }
        
        root_view.addSubview(headerLab)
        headerLab.snp.makeConstraints{(make) in
            make.centerY.height.equalTo(onlyLab)
            make.centerX.equalTo(headframeImg)
        }
        
        root_view.addSubview(headerPlaceLab)
        headerPlaceLab.snp.makeConstraints{(make) in
            make.centerY.width.equalTo(onlyPlaceLab)
            make.centerX.equalTo(headerLab)
        }
        
        root_view.addSubview(senderBtn)
        senderBtn.snp.makeConstraints{(make) in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-20-15-BTSafeAreaBottom)
            make.width.equalTo(295)
            make.height.equalTo(52)
        }
        
        bind_data()
        // Do any additional setup after loading the view.
    }
    
    fileprivate func bind_data(){
        
        guard let m = dataModel,let user = ST_BTUserInfoManager.shared.current_account_info?.userinfo else { return }
        
        
        
        meHeadImg.pin_setImage(from: user.avatar.compress_small_image(width: 86*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        herHeadImg.pin_setImage(from: m.herAvatar.compress_small_image(width: 86*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        headerImg.pin_setImage(from: m.herAvatar.compress_small_image(width: 86*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        let p = NSParagraphStyle.create_line_spacing_paragraph_style(lineSpacing: 3)
        
        p.alignment = .center
     
        
        let normal_attribute = [Attribute.font(Font.systemFont(ofSize: 15, weight: UIFont.Weight.semibold)),Attribute.textColor(UIColor.init(hex: 0x333333)!),.paragraphStyle(p)]
        
        
        let highlineAttribute = [Attribute.font(Font.systemFont(ofSize: 15, weight: UIFont.Weight.semibold)),Attribute.textColor(UIColor.init(hex: 0xFEC207)!),.paragraphStyle(p)]
        
        
        //        有人守护中
        if m.guardTime.double > Date().timeIntervalSince1970{
            
            let str = "赠送至少".withAttributes(normal_attribute)

            str.append("\(m.guardNextCoin)甜心币".withAttributes(highlineAttribute))

            str.append("的守护即可取而代之，\n成为\(ta)的唯一甜蜜守护。".withAttributes(normal_attribute))

            
            titleLab.attributedText = str
        
            if ta.contains("她"){
                senderBtn.setTitleForAllStates("我要取代他！")
            }else{
                senderBtn.setTitleForAllStates("我要取代她！")
            }
            
            
            
        }else{
            
            //            titleLabel.text = "成为唯一守护帮你人工牵线"
            //
            //            let str =  "喜欢不用等，成为唯一守护！红娘立刻帮你认识！线下真实见面机会增加10倍！".withAttributes(normal_attribute)
            //
            //            str.addAttributes(paragraphAttribute, range: NSRange.init(location: 0, length: str.length))
            //
            //            content_label.attributedText = str
            
            titleLab.attributedText = "成为\(ta)的唯一甜蜜守护，尊享独一无二特权".withAttributes(normal_attribute)
            
            senderBtn.setTitleForAllStates("立刻成为\(ta)的守护")
            
        }
        
        
        
        
    }
    
    
    // MARK: - Properties
    var dataModel:PairsGuardModel?
    
    var ta = ""
    
    fileprivate lazy var root_view:UIView = {
        let control = UIView()
        control.backgroundColor = UIColor.white
        control.cornerRadius = 15
        
        return control
    }()
    
    
    fileprivate lazy var close_view:UIControl = {
        let control = UIControl()
        control.addTarget(self, action: #selector(close_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var topbackImg:UIImageView = {
        let imgView = UIImageView()
        imgView.image = ST_ImageUtils.imageName("guardHerTopBackImg")
        
        return imgView
    }()
    
    fileprivate lazy var heart_image_view:UIImageView = {
        let control = UIImageView()
        control.image = ST_ImageUtils.imageName("gurdeSuccessIcon")
        
        return control
    }()
    
    fileprivate lazy var herHeadImg:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.cornerRadius = 43
        return control
    }()
    
    fileprivate lazy var meHeadImg:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.cornerRadius = 43
        return control
    }()
    
    fileprivate lazy var titleLab:UILabel = {
        let control = UILabel()
        control.numberOfLines = 0
        return control
    }()
    
    fileprivate lazy var leftImg:UIImageView = {
        let control = UIImageView()
        control.image = ST_ImageUtils.imageName("guardHerNowLeftImg")
        return control
    }()
    
    fileprivate lazy var rightImg:UIImageView = {
        let control = UIImageView()
        control.image = ST_ImageUtils.imageName("guardHerNowRightImg")
        return control
    }()
    
    fileprivate lazy var onlyImg:UIImageView = {
        let control = UIImageView()
        control.image = ST_ImageUtils.imageName("guarded_only_you")
        return control
    }()
    
    fileprivate lazy var onlyLab:UILabel = {
        let control = UILabel()
        control.textColor = UIColor.init(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 14)
        control.text = "唯一占有"
        control.textAlignment = .center
        return control
    }()
    
    fileprivate lazy var onlyPlaceLab:UILabel = {
        let control = UILabel()
        control.font = UIFont.systemFont(ofSize: 13)
        control.textColor = UIColor.init(hexString: "#ACB4C0")
        control.numberOfLines = 2
        control.text = "告诉全网她是你的唯一"
        control.textAlignment = .center
        return control
    }()
    
    fileprivate lazy var topImg:UIImageView = {
        let control = UIImageView()
        control.image = ST_ImageUtils.imageName("guarded_top_chat")
        return control
    }()
    
    fileprivate lazy var topLab:UILabel = {
        let control = UILabel()
        control.textColor = UIColor.init(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 14)
        control.textAlignment = .center
        control.text = "聊天置顶"
        return control
    }()
    
    fileprivate lazy var topPlaceLab:UILabel = {
        let control = UILabel()
        control.textColor = UIColor.init(hexString: "#ACB4C0")
        control.font = UIFont.systemFont(ofSize: 14)
        control.textAlignment = .center
        control.numberOfLines = 2
        control.text = "在她的聊天列表置顶"
        return control
    }()
    
    fileprivate lazy var headframeImg:UIImageView = {
        let control = UIImageView()
        control.image = ST_ImageUtils.imageName("guardHerNowHeadframeImg")
        return control
    }()
    
    fileprivate lazy var headerImg:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.cornerRadius = 30
        return control
    }()
    
    fileprivate lazy var headerLab:UILabel = {
        let control = UILabel()
        control.textColor = UIColor.init(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 14)
        control.textAlignment = .center
        control.text = "头像装饰"
        return control
    }()
    
    fileprivate lazy var headerPlaceLab:UILabel = {
        let control = UILabel()
        control.textColor = UIColor.init(hexString: "#ACB4C0")
        control.font = UIFont.systemFont(ofSize: 14)
        control.textAlignment = .center
        control.text = "为她定制专属头像框"
        control.numberOfLines = 2
        return control
    }()
    
    fileprivate lazy var senderBtn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        control.cornerRadius = 26
        control.setTitleColor(UIColor.white, for: UIControl.State.normal)
        control.backgroundColor = UIColor.init(hexString: "#FEC207")
        control.addTarget(self, action: #selector(confirm_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    var sendGiftClosure:(()->())?
    
    @objc fileprivate func confirm_handle(){
        
        goback_previous_controller(animated: false) {
            
            self.sendGiftClosure?()
        }
        
    }
    
    @objc fileprivate func cancel_handle(){
        
        goback_previous_controller(animated: false)
    }
    
    
    @objc fileprivate func close_handle(){
        
        goback_previous_controller()
        
    }
}






